from nicegui import app, ui
from tools.search import Search
from tools.loginout import logout
from orm import manager
from tools.message import *

def publish():
    def save_publish():
        app.storage.general["公告"] = app.storage.user['input_publish']
        dialog.close()

    with ui.dialog() as dialog, ui.card().tight().classes('w-[400px] h-[300px]'):
        with ui.column().classes("w-full h-full"):
            ui.textarea("发布公告").bind_value_to(app.storage.user, "input_publish").classes("w-full h-4/5")
            ui.button("发布！", on_click=lambda: save_publish(), color='text-indigo-700')\
                .classes("w-full grow bg-gradient-to-br from-purple-200 to-blue-200")
    dialog.open()


def init_page(left_drawer=True):
    with ui.header(elevated=True) \
            .classes('bg-gradient-to-br from-blue-400 to-purple-300'):
        with ui.row().classes("w-full"):
            ui.label("星汉 Starry Sky").classes('text-2xl font-bold tracking-wide font-serif text-white italic')
            with ui.row().classes("grow justify-end"):
                search = Search()
                search.create_button()
                ui.button("side", on_click=lambda: leftDrawer.toggle(), color='text-indigo-700', icon="menu") \
                    .classes('mr-1 bg-gradient-to-br from-purple-500 to-blue-300 grow-0')
                ui.button("个人信息 ", on_click=lambda: ui.open("/personal_information"), color='text-indigo-700',
                          icon='person').classes('mr-1 bg-gradient-to-br from-purple-500 to-blue-300 grow-0')
                ui.button("退出   ", on_click=logout, color='text-indigo-700',
                          icon='logout').classes('mr-1 bg-gradient-to-br from-purple-500 to-blue-300 grow-0')
    if left_drawer:
        with ui.left_drawer(top_corner=False, bottom_corner=True) \
                .classes('bg-gradient-to-r from-blue-100 to-purple-50') as leftDrawer:
            with ui.column():
                ui.button("首页   ", on_click=lambda: ui.open("/user"), color='text-indigo-700', icon='home') \
                    .classes('bg-gradient-to-br from-purple-200 to-blue-200 w-full')
                ui.button("我的用户组", on_click=lambda: ui.open("/user_groups"), color='text-indigo-700',
                          icon='group').classes('bg-gradient-to-br from-purple-200 to-blue-200 w-full')
                ui.button("我的问题组", on_click=lambda: ui.open("/question_groups"), color='text-indigo-700',
                          icon='receipt_long').classes('bg-gradient-to-br from-purple-200 to-blue-200 w-full')
                ui.button("给以后的自己发送信息", on_click=lambda: MessageSend(manager.get_user(app.storage.user["username"]))
                          , color='text-indigo-700', icon='receipt_long').classes(
                    'bg-gradient-to-br from-purple-200 to-blue-200 w-full')
                if manager.is_administrator(app.storage.user.get('username')):
                    ui.button("敏感问题组", on_click=lambda: ui.open("/sensitive_questions"), color='text-indigo-700',
                              icon='warning').classes('bg-gradient-to-br from-purple-200 to-blue-200 w-full')
                    ui.button("发布公告", on_click=lambda: publish(), color='text-indigo-700', icon="publish") \
                        .classes('bg-gradient-to-br from-purple-200 to-blue-200 w-full')
    ui.query('body').classes('bg-gradient-to-r from-blue-100 to-white')
    MessageReceive()
